package Json_demo;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;

import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.type.TypeBindings;
import org.codehaus.jackson.type.TypeReference;

public class Json_Read_Hashmap {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
// khoi tao thu vien Json objmapper
		ObjectMapper objma = new ObjectMapper();
//bat cua so chon file lay duong dan
		JFileChooser objFile = new JFileChooser();
        objFile.setCurrentDirectory(new java.io.File("."));
        objFile.setDialogTitle("Chọn file: ");
        objFile.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
        String[] EXTENSION = new String[] { "json" };
        FileNameExtensionFilter filter = new FileNameExtensionFilter("JSON files", EXTENSION);
        objFile.setFileFilter(filter);
        objFile.setMultiSelectionEnabled(false);

        int returnVal = objFile.showOpenDialog(null);
        String ParentPath = "";
        String FileName = "";
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            // ParentPath = objFile.getSelectedFile().getParent();
            FileName = objFile.getSelectedFile().getPath();
 // khoi tao 1 hashmap ghi lai ket qua thu duoc tu json
            try {
				
            	HashMap<Integer, Teacher> HasmapKQ = objma.readValue(new File(FileName), new TypeReference<HashMap<Integer, Teacher>>() {});
				

				for (int i = 0 ; i<HasmapKQ.size();i++){
					Teacher lisKQ = HasmapKQ.get(i);
					System.out.println("----Teacher"+i+"------");
					System.out.println("RollNo :"+lisKQ.getRollNo());
					System.out.println("FullName :"+lisKQ.getFullName());
					System.out.println("Mon day :"+lisKQ.getMon());
					System.out.println("Diem so :"+lisKQ.getDiemSo());
					
				}
				
            
            } catch (JsonParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (JsonMappingException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }
	}

}
